package com.qboot.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.qboot.common.annotation.Log;
import com.qboot.common.consts.MsgConst;
import com.qboot.common.controller.BaseController;
import com.qboot.common.entity.Result;
import com.qboot.common.entity.TableResult;
import com.qboot.common.utils.EasyPoiExcelUtils;
import com.qboot.system.entity.SysDictItem;
import com.qboot.system.service.SysDictItemService;
import com.qboot.system.service.SysDictService;
import com.qboot.system.vo.SysDictItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 字典选项
 *
 * @author Tellsea
 * @date 2022-12-17
 */
@Api(tags = "字典选项")
@RestController
@RequestMapping("/system/sysDictItem")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysDictItemController extends BaseController {

    private final SysDictItemService sysDictItemService;
    private final SysDictService sysDictService;

    @ApiOperation("字典选项查询")
    @SaCheckPermission("system:sysDictItem:list")
    @GetMapping("/list")
    public TableResult<SysDictItemVo> queryList(SysDictItemVo entity) {
        return TableResult.ok(sysDictItemService.queryList(getPage(), entity));
    }

    @ApiOperation("字典选项查询全部")
    @SaCheckPermission("system:sysDictItem:list")
    @GetMapping("/listAll")
    public TableResult<SysDictItemVo> listAll(SysDictItemVo entity) {
        return TableResult.ok(sysDictItemService.queryList(getAllPage(), entity));
    }

    @ApiOperation("字典选项导入")
    @Log("字典选项导入")
    @SaCheckPermission("system:sysDictItem:import")
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file) {
        List<SysDictItem> list = EasyPoiExcelUtils.importExcel(file, 1, 1, SysDictItem.class);
        sysDictItemService.saveBatch(list, list.size());
        return Result.ok(MsgConst.IMPORT_SUCCESS + list.size() + "条数据");
    }

    @ApiOperation("字典选项导出")
    @Log("字典选项导出")
    @SaCheckPermission("system:sysDictItem:export")
    @GetMapping("/exportExcel")
    public void exportExcel(SysDictItemVo entity) {
        List<SysDictItemVo> list = sysDictItemService.queryList(getAllPage(), entity).getRecords();
        EasyPoiExcelUtils.exportExcel(list, "字典选项-" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"), SysDictItemVo.class);
    }

    @ApiOperation("字典选项根据主键查询")
    @Log("字典选项根据主键查询")
    @SaCheckPermission("system:sysDictItem:get")
    @GetMapping("/queryById/{id}")
    public Result<SysDictItemVo> queryById(@PathVariable("id") Long id) {
        return queryResult(sysDictItemService.queryById(id));
    }

    @ApiOperation("字典选项新增")
    @Log("字典选项新增")
    @SaCheckPermission("system:sysDictItem:save")
    @PostMapping("/save")
    public Result save(@RequestBody SysDictItemVo entity) {
        entity.setDictCode(sysDictService.getById(entity.getDictId()).getDictCode());
        return saveResult(sysDictItemService.save(entity));
    }

    @ApiOperation("字典选项根据主键更新")
    @Log("字典选项根据主键更新")
    @SaCheckPermission("system:sysDictItem:update")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody SysDictItemVo entity) {
        entity.setDictCode(sysDictService.getById(entity.getDictId()).getDictCode());
        return updateResult(sysDictItemService.updateById(entity));
    }

    @ApiOperation("字典选项根据主键删除")
    @Log("字典选项根据主键删除")
    @SaCheckPermission("system:sysDictItem:remove")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        return removeResult(sysDictItemService.removeById(id));
    }

    @ApiOperation("字典选项根据主键批量删除")
    @Log("字典选项根据主键批量删除")
    @SaCheckPermission("system:sysDictItem:removeBatch")
    @GetMapping("/removeBatchByIds/{ids}")
    public Result removeBatchByIds(@PathVariable("ids") List<Long> ids) {
        return removeResult(sysDictItemService.removeBatchByIds(ids));
    }
}
